using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// Zune.Forms CheckBox control
    /// </summary>
    public class CheckBox : Control
    {
        #region Public Events
        /// <summary>
        /// Fires when the CheckBox's check state changes.
        /// </summary>
        public event EventHandler CheckedChanged;
        #endregion

        #region Private Members
        private Texture2D _vLine;
        private Texture2D _hLine;
        private Texture2D _checkMark;
        private string _text = "Label";
        private SpriteFont _font;
        private bool _displayBorder = true;
        private int _borderWidth = 2;
        private Color _borderColor = Color.Black;
        private Color _cbBackgroundColor = Color.White;
        private bool _autoSize = true;
        private Vector2 _checkBoxSize = new Vector2(30, 30);
        private bool _isChecked = false;
        private bool _isPressed = false;
        #endregion

        #region Constructors
        /// <summary>
        /// Creates a new CheckBox conrol
        /// </summary>
        /// <param name="form"></param>
        public CheckBox(Form form) : base(form)
        {
            base.Size = new Vector2(100, 30);
            base.Location = new Vector2(0, 0);
            base.ForeColor = Color.White;
        }
        #endregion

        #region Public Overrides
        public override void LoadContent()
        {
            _font = base.Form.Content.Load<SpriteFont>("DefaultFont");
            _checkMark = base.Form.Content.Load<Texture2D>("CheckMark");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (base.Visible)
            {
                _hLine = Drawing.CreateLine(base.GraphicsDevice, 0, 0, (Int32)_checkBoxSize.X, _borderWidth, _borderColor);
                _vLine = Drawing.CreateLine(base.GraphicsDevice, 0, 0, _borderWidth, (Int32)_checkBoxSize.Y, _borderColor);

                Vector2 textSize = _font.MeasureString(_text);

                if (_autoSize)
                {
                    _size = new Vector2(textSize.X + 4 + _checkBoxSize.X + (_borderWidth * 2), textSize.Y + 4 + (_borderWidth * 2));
                    _hLine = Drawing.CreateLine(base.GraphicsDevice, 0, 0, (Int32)_checkBoxSize.X, _borderWidth, _borderColor);
                    _vLine = Drawing.CreateLine(base.GraphicsDevice, 0, 0, _borderWidth, (Int32)_checkBoxSize.Y, _borderColor);
                }

                Texture2D background = Drawing.CreateRectangle(base.GraphicsDevice, (Int32)base.Size.X, (Int32)base.Size.Y, _backColor);
                base.SpriteBatch.Draw(background, base.Location, Color.White);

                float yStart = base.Location.Y + base.Size.Y / 2 - _checkBoxSize.Y / 2;
                if (_isPressed)
                {
                    Texture2D pressed = Drawing.CreateRectangle(base.GraphicsDevice, (Int32)_checkBoxSize.X - _borderWidth, (Int32)_checkBoxSize.Y - _borderWidth, _cbBackgroundColor);
                    base.SpriteBatch.Draw(pressed, new Vector2(base.Location.X + (float)_borderWidth, yStart + (float)_borderWidth), Color.White);
                }

                //Checkbox layout
                base.SpriteBatch.Draw(_hLine, new Vector2(base.Location.X, yStart), Color.White);
                base.SpriteBatch.Draw(_hLine, new Vector2(base.Location.X, yStart + _checkBoxSize.Y - _borderWidth), Color.White);
                base.SpriteBatch.Draw(_vLine, new Vector2(base.Location.X, yStart), Color.White);
                base.SpriteBatch.Draw(_vLine, new Vector2(base.Location.X + _checkBoxSize.X - _borderWidth, yStart), Color.White);

                if (_isChecked)
                {
                    //Draw the checmark:
                    base.SpriteBatch.Draw(_checkMark, new Rectangle((Int32)base.Location.X + _borderWidth + 1, (Int32)yStart + _borderWidth + 1, (Int32)_checkBoxSize.X - _borderWidth - 4, (Int32)_checkBoxSize.Y - _borderWidth - 4), Color.White);
                }

                Vector2 textLocation = new Vector2(base.Location.X + 4 + _checkBoxSize.X, base.Location.Y + 2 + _borderWidth);
                base.SpriteBatch.DrawString(_font, _text, textLocation, _foreColor);

                //base.Draw(gameTime);
            }
        }

        public override void Update(GameTime gameTime, TouchCollection touches)
        {
            if (base.Visible)
            {
                if (touches.Count > 0)
                {
                    TouchLocation touch = touches[0];
                    if (Utilities.IsControlTouched(touch.Position, base.Location, base.Size) && touch.Pressure > Constants.CHECKED_PRESSURE)
                    {
                        switch (touch.State)
                        {
                            case TouchLocationState.Released:
                                if (_isChecked)
                                {
                                    _isChecked = false;
                                }
                                else
                                {
                                    _isChecked = true;
                                }
                                
                                _isPressed = false;

                                if (CheckedChanged != null)
                                    CheckedChanged(this, EventArgs.Empty);
                                
                                break;
                            case TouchLocationState.Pressed:
                            case TouchLocationState.Moved:
                                _isPressed = true;
                                break;
                        }
                    }
                    else
                    {
                        _isPressed = false;
                    }
                }
                base.Update(gameTime, touches);
            }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the Text
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Gets or sets the Color to use for the border.
        /// </summary>
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }

        /// <summary>
        /// Gets or sets the width of the border.
        /// </summary>
        public int BorderWidth
        {
            get { return _borderWidth; }
            set { _borderWidth = value; }
        }

        /// <summary>
        /// Gets or sets whether the CheckBox is checked.
        /// </summary>
        public bool Checked
        {
            get { return _isChecked; }
            set { _isChecked = value; }
        }
        #endregion
    }
}